package web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import dto.RoleGroupDto;
import entity.Admin;
import entity.Group_Menu;
import entity.RoleGroup;
import enums.travelenums;
import service.RoleGroup_service;

@Controller
public class RoleGroup_controller {

	@Autowired
	private RoleGroup_service group_service;
	
	//角色列表
	@RequestMapping(value="grouplist",method=RequestMethod.POST)
	@ResponseBody
	public List<RoleGroupDto> GroupList(){
		return group_service.Group_list();
	}
	
	/*选中角色显示对应菜单*/
	@RequestMapping(value="group_menu",method=RequestMethod.POST)
	@ResponseBody
	public List<Group_Menu> group_menu(Integer groupId){
		return group_service.Group_MenuList(groupId);
	}
	
	//分配菜单
	@RequestMapping(value="group/menu",method=RequestMethod.POST)
	@ResponseBody
	public String GroupMenu(Integer groupId,String menuIds){
		if(group_service.assignMenu(groupId, menuIds)){
			return travelenums.SUCCESS.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	//新增或修改角色
	@RequestMapping(value="group/add_update",method=RequestMethod.POST)
	@ResponseBody
	public String AddOrUpdateRole(RoleGroup roleGroup){
		RoleGroup Sql_rolegroup=group_service.getRoleGroupById(roleGroup.getId());
		if(Sql_rolegroup!=null){
			Sql_rolegroup.setRoleName(roleGroup.getRoleName());
			boolean boo=group_service.update_RoleGroup(Sql_rolegroup);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}else{
			boolean boo=group_service.add_RoleGroup(roleGroup);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
	}
	//删除角色
	@RequestMapping(value="group/delete",method=RequestMethod.POST)
	@ResponseBody
	public String RoleDelete(RoleGroup roleGroup){
		RoleGroup Sql_roleGroup=group_service.getRoleGroupById(roleGroup.getId());
		if(Sql_roleGroup!=null){
			boolean boo=group_service.delete_RoleGroup(roleGroup.getId());
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
}
